﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

namespace Baosight.Ccs.Mail.Handlers
{
    public class CompositeHandler : IMailHandler
    {

        private List<IMailHandler> handlers = new List<IMailHandler>();

        public List<IMailHandler> Handlers
        {
            get { return handlers; }
            set { handlers = value; }
        }

        #region IMailHandler Members

        public bool Process(Mail mail)
        {
            foreach (IMailHandler handler in handlers)
            {
                try
                {
                    if (handler.Process(mail))
                    {
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    try
                    {
                        MailManager.Instance.ChangeMailStatus(mail, MailStatus.Error);
                    }
                    catch { }
                    ExceptionPolicy.HandleException(ex, MailConstants.MailHandlerExceptionPolicy);
                    return true;
                }
            }
            return false;
        }

        #endregion
    }
}
